package com.garlic.face.jlf.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Created by shiyanqi on 18/8/20.
 */

public class ConfigActivity extends BaseActivity {

    private EditText mShopno;
    private EditText mPosno;
    private EditText mSocketIp;
    private EditText mSocketPort;
    private EditText mDeviceNum;
    private Button closeConfig;
    public static final String CONFIG_DONE = "CONFIG_DONE";
    private LinearLayout configLayout;
    private InputMethodManager inputMethodManager; // 软键盘

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        Button btnOk = findViewById(R.id.setting_OK_button);
        mShopno = findViewById(R.id.setting_shop_name);
        mPosno = findViewById(R.id.setting_posno);
        mSocketIp = findViewById(R.id.setting_ip);
        mSocketPort = findViewById(R.id.setting_port);
        closeConfig = findViewById(R.id.config_close);
        configLayout = findViewById(R.id.config_layout);
        mDeviceNum = findViewById(R.id.setting_deviceNum);
		/*mShopno.setText("98");
        mPosno.setText("1");
		mSocketIp.setText("10.132.255.170");
		mSocketPort.setText("2000");*/
        String shopNo = (String) Tools.getPref(App.CONFIG_INFO, App.SHOP_NO, "");
        if (!TextUtils.isEmpty(shopNo)) {
            mShopno.setText(shopNo);
        }
        String posNo = (String) Tools.getPref(App.CONFIG_INFO, App.POS_NO, "");
        if (!TextUtils.isEmpty(posNo)) {
            mPosno.setText(posNo);
        }
        String posIp = (String) Tools.getPref(App.CONFIG_INFO, App.POS_IP, "");
        if (!TextUtils.isEmpty(posIp)) {
            mSocketIp.setText(posIp);
        }
        String deviceNum = (String) Tools.getPref(App.CONFIG_INFO, App.DEVICE_NUM, "");
        if(!TextUtils.isEmpty(deviceNum)){
            mDeviceNum.setText(deviceNum);
        }

        int port = (int) Tools.getPref(App.CONFIG_INFO, App.POS_PORT, 0);
        if (port != 0) {
            mSocketPort.setText(String.valueOf(port));
        }
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doConfig();
            }
        });
        closeConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mShopno.setInputType(InputType.TYPE_CLASS_NUMBER);
        mPosno.setInputType(InputType.TYPE_CLASS_NUMBER);
        mDeviceNum.setInputType(InputType.TYPE_CLASS_NUMBER);
        mSocketPort.setInputType(InputType.TYPE_CLASS_NUMBER);
        inputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        configLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput();
            }
        });
    }

    private void doConfig() {
        String shopNo = mShopno.getText().toString().trim();
        String posNo = mPosno.getText().toString().trim();
        String socketIp = mSocketIp.getText().toString().trim();
        String socketPort = mSocketPort.getText().toString().trim();
        String deviceNum = mDeviceNum.getText().toString().trim();
        if (informationIsOk(shopNo, posNo, socketIp, socketPort, deviceNum)) { // 检验通过
            hideSoftInput();
            saveStore(shopNo, posNo, socketIp, Integer.parseInt(socketPort), deviceNum);
            finish();
        }
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    private boolean informationIsOk(String shopNo, String posNo, String ipAdd, String port, String deviceNum) {
        boolean ret = true;
        StringBuffer sb = new StringBuffer("");
        if (shopNo.length() <= 0) {
            ret = false;
            sb.append("门店编号不能为空 ");
        }
        if (posNo.length() <= 0) {
            ret = false;
            sb.append("站点编号不能为空 ");
        }
        if (ipAdd.length() <= 0) {
            ret = false;
            sb.append("wincor POS IP地址不能为空 ");
        }
        if (port.length() <= 0) {
            ret = false;
            sb.append("wincor POS 端口不能为空 ");
        }
        if (deviceNum.length() <= 0) {
            ret = false;
            sb.append("机具号不能为空 ");
        }
        // TODO: 2018/7/16 这里要判断IP和Port是否正确，可以调用getState获取POS状态
        if (!ret) {
            ToastUtils.toastShow(sb.toString().trim(), ToastUtils.ERROR);
        }
        return ret;
    }

    /**
     * 存储门店数据
     */
    private void saveStore(String shopNo, String posNo, String Ip, int Port, String deviceNum) {
        //String prefShopName = (String) Tools.getPref(App.CONFIG_INFO, App.SHOP_NAME, "");
        //String prefShopNo = (String) Tools.getPref(App.CONFIG_INFO, App.SHOP_NO, "");
        //String prefPosNo = (String) Tools.getPref(App.CONFIG_INFO, App.POS_NO, "");
        String prefPosIp = (String) Tools.getPref(App.CONFIG_INFO, App.POS_IP, "");
        int prefPosPort = (int) Tools.getPref(App.CONFIG_INFO, App.POS_PORT, 0);

        /*if(shopName.equals(prefShopName) && shopNo.equals(prefShopNo) && posNo.equals(prefPosNo)
                && Ip.equals(prefPosIp) && Port == prefPosPort){
            return;
        }*/

        // TODO: 2018/7/16 保存posNo等设置信息到文件中，方便后期升级
        Means.getInstance().setShopno(shopNo);
        Means.getInstance().setPosno(posNo);
        Means.getInstance().setPosIp(Ip);
        Means.getInstance().setPosPort(Port);
        Means.getInstance().setDeviceNum(deviceNum);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(App.SHOP_NO, shopNo);
        map.put(App.POS_NO, posNo);
        map.put(App.POS_IP, Ip);
        map.put(App.POS_PORT, Port);
        map.put(App.DEVICE_NUM, deviceNum);
        Tools.putMapPref(App.CONFIG_INFO, map);

        //如果ip或端口有变化则开始重启服务
        if(!Ip.equals(prefPosIp) || Port != prefPosPort){
            EventBus.getDefault().post(CONFIG_DONE);
        }

    }

    /**
     * 隐藏软键盘
     */
    private void hideSoftInput(){
        if (inputMethodManager != null) {
            // 参数1：当前viewTree任意一个view的windowToken     参数2：没要求0即可
            inputMethodManager.hideSoftInputFromWindow(mShopno.getWindowToken(), 0);
        }
    }

}
